package com.xiaoshuidi.cloud.module.iot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.dal.access.DeviceOperationDataAccess;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceOperation;
import com.xiaoshuidi.cloud.module.iot.service.InteceptorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <br>
 *
 * @author Porter
 * create by 2019/7/9
 * @Emial Porter.Wang@ixiaoshuidi.com
 */
@Service
public class InteceptorServiceImpl implements InteceptorService {

    @Autowired
    private DeviceOperationDataAccess deviceOperationDataAccess;

    @Override
    public Integer addOperateRecord(String deviceNo, Integer opType) {
        TbDeviceOperation tbDeviceOperation = new TbDeviceOperation();
        tbDeviceOperation.setDeviceNo(deviceNo);
        tbDeviceOperation.setOpType(opType);
        tbDeviceOperation.setOpTime(new Date());
        tbDeviceOperation.setMemberType("0");
        tbDeviceOperation.setCreateTime(LocalDateTime.now());
        tbDeviceOperation.setUpdateTime(LocalDateTime.now());
        tbDeviceOperation.setStatus(1);
        return deviceOperationDataAccess.insterDeviceOperationRecord(tbDeviceOperation);
    }

    @Override
    public Boolean updateOperateRecord(JSONObject jsonObject) {
        TbDeviceOperation tbDeviceOperation = new TbDeviceOperation();
        tbDeviceOperation.setId(jsonObject.getInteger("operateRecordId"));
        tbDeviceOperation.setMemberType("1");
        tbDeviceOperation.setMemberName(jsonObject.getString("thirdPassId"));
        return deviceOperationDataAccess.updateDeviceOperationRecord(tbDeviceOperation);
    }

    @Override
    public Boolean deleteOperateRecord(String deviceNo) {
        return deviceOperationDataAccess.deleteOperateRecord(deviceNo);
    }
}
